package ru.niir.goodfil.ui;

import ru.niir.goodfil.db.Good;
import ru.niir.goodfil.db.GoodType;
import ru.niir.goodfil.exceptions.GoodfilSecurityException;
import ru.niir.goodfil.exceptions.ValidationException;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Волков Олег on 22.08.14.
 */
public class BalanseReportBean extends PageBean {

    private int value = 0;

    private Good good = new Good();

    private int rowsCount = 50;

    private String coment;

    private boolean allRows;

    private BigInteger salesForMonth = BigInteger.valueOf(0);

    private BigInteger salesForThreeMonths = BigInteger.valueOf(0);

    private BigInteger salesForSixMonths = BigInteger.valueOf(0);

    private BigInteger salesForYear = BigInteger.valueOf(0);

    public List<Good> getGoods() throws ValidationException {
        List<GoodType> goodTypes = gf().getGoodTypes();
        List<Good> goods = gf().getGoods(getSessionBean().getGoodsFilter());
        List<Good> result = new ArrayList<Good>();
        for (Good good : goods) {
            if (good.isDeleted()) {
                continue;
            }

            for (GoodType goodType : goodTypes) {
                if (goodType.getId() == good.getGoodTypeId()) {
                    good.setGoodType(goodType);
                }
            }
            //  Получение данных о том, выбран ли данный товар
            good.setSelected(getSessionBean().getSelectedGoods().contains(good.getId()));
            result.add(good);
        }
        return result;
    }

    public void setNewDangerousRest() {
        if (!getSecurityBean().getIsRoleAdministrator())
            throw new GoodfilSecurityException("Только администраторы имеют доступ к отчету по остаткам");
        try {
            gf().updateGood(good);
        } catch (ValidationException e) {
            e.printStackTrace();
        }
        clearAll();
    }

    public void showAllRows() {
        if (!isAllRows()) {
            try {
                setRowsCount(getGoods().size());
                setAllRows(true);
            } catch (ValidationException e) {
                e.printStackTrace();
            }
        } else {
            setAllRows(false);
            setRowsCount(50);
        }
    }

    public void clearAll() {
        good = new Good();
        value = 0;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Good getGood() {
        return good;
    }

    public void setGood(Good good) {
        this.good = good;
    }

    public int getRowsCount() {
        return rowsCount;
    }

    public void setRowsCount(int rowsCount) {
        this.rowsCount = rowsCount;
    }

    public String getComent() {
        return coment;
    }

    public void setComent(String coment) {
        this.coment = coment;
    }

    public BigInteger getSalesForMonth() {
        salesForMonth = gf().getGoodSalesForMonth(good);
        return salesForMonth;
    }

    public BigInteger getSalesForThreeMonths() {
        salesForThreeMonths = gf().getGoodSalesForThreeMonths(good);
        return salesForThreeMonths;
    }

    public BigInteger getSalesForSixMonths() {
        salesForSixMonths = gf().getGoodSalesForSixMonths(good);
        return salesForSixMonths;
    }

    public BigInteger getSalesForYear() {
        salesForYear = gf().getGoodSalesForYear(good);
        return salesForYear;
    }

    public boolean isAllRows() {
        return allRows;
    }

    public void setAllRows(boolean allRows) {
        this.allRows = allRows;
    }
}
